import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
  list = '/FinancialInvoice/financialInvoice/list',
  save='/FinancialInvoice/financialInvoice/add',
  edit='/FinancialInvoice/financialInvoice/edit',
  deleteOne = '/FinancialInvoice/financialInvoice/delete',
  deleteBatch = '/FinancialInvoice/financialInvoice/deleteBatch',
  importExcel = '/FinancialInvoice/financialInvoice/importExcel',
  exportXls = '/FinancialInvoice/financialInvoice/exportXls',
  deleteFile = '/FinancialInvoice/financialInvoice/deleteFile',
  uploadInvoiceFile = '/FinancialInvoice/financialInvoice/uploadInvoiceFile',
  previewFile = '/FinancialInvoice/financialInvoice/previewFile',
  downloadFile = '/FinancialInvoice/financialInvoice/downloadFile',
  getFiles = '/FinancialInvoice/financialInvoice/getFiles',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}

/**
 * 保存或者更新
 * @param params
 * @param isUpdate
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
}

/**
 * 删除财务文件
 * @param fileId 文件ID
 */
export const deleteFinancialFile = (fileId) => {
  return defHttp.delete(
    {
      url: Api.deleteFile,
      params: { fileId },
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 上传财务文件
 * @param file 文件
 * @param id 记录ID
 */
export const uploadFinancialFile = (file, id) => {
  return defHttp.post(
    {
      url: Api.uploadInvoiceFile,
      params: { file, id },  
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 预览文件
 * @param fileId 文件ID
 */
export const previewFile = (fileId) => {
  return defHttp.get(
    {
      url: Api.previewFile,
      params: { fileId },
      responseType: 'blob'
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 下载文件
 * @param fileId 文件ID
 */
export const downloadFile = (fileId) => {
  return defHttp.get(
    {
      url: Api.downloadFile,
      params: { fileId },
      responseType: 'blob'
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};

/**
 * 获取文件列表
 * @param id 记录ID
 */
export const getFiles = (id) => {
  return defHttp.get(
    {
      url: Api.getFiles,
      params: { id },
    },
    { joinParamsToUrl: true, isTransformResponse: false }
  );
};